﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 任务状态
{
    public class ProgressBarDrawer
    {
        private int Width;
        private int Height;
        private Panel panel1;
        public ProgressBarDrawer(Panel panel)
        {
            this.Width = panel.Width;
            this.Height = panel.Height;
            this.panel1 = panel;
        }

        public void SetBar(float d1, float data,Color[] color)
        {
            if(d1==0)
            {
                Console.WriteLine("SetBar d1 不能等于0");
                return;
            }
            Color bcolor = color[2];
            Color fcolor = color[0];
            if (data < 0)
            {
                bcolor = color[3];
                fcolor = color[1];
            }
            using (Bitmap progressBitmap = new Bitmap(this.Width, this.Height))
            {
                using (Graphics g = Graphics.FromImage(progressBitmap))
                {
                    g.Clear(bcolor);
                    int barHeight = (int)((Math.Abs(data) / d1 * 10) * this.Height);

                    Rectangle barRect;
                    if (data >= 0)
                    {
                        barRect = new Rectangle(0, this.Height - barHeight, this.Width, barHeight);
                    }
                    else
                    {
                        barRect = new Rectangle(0, 0, this.Width, barHeight);
                    }
                    g.FillRectangle(new SolidBrush(fcolor), barRect);
                }
                panel1.BackgroundImage = (Bitmap)progressBitmap.Clone();
            }
        }
    }

}

